/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：Tournament.java <br>
 * 创建时间：2014-1-25 下午9:04:54 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.battle.proc.holder;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Collections;

import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.battle.BattleScreen;
import lostland.gmud.exv2.blgframework.CScreen;
import lostland.gmud.exv2.expand.GeneralLookScreen;
import lostland.gmud.exv2.expand.GeneralMenuScreen;
import lostland.gmud.exv2.expand.vs;
import lostland.gmud.exv2.ui.NotificationScreen;
import lostland.gmud.exv2.ui.TalkingScreen;
import lostland.gmud.exv2.ui.core.YesNoScreen;

/**
 * 类名：Tournament <br>
 * 说明：武林大会
 *
 * @author 12548
 */
public class Tournament extends BattleHolder {

	ArrayList<Integer> players;
	ArrayList<vs> vses;

	int system;

	boolean left;
	boolean circle_over = false;


	final int SYSTEM_ELIMINATION = 0;

	public vs match;


	public Tournament() {
		players = new ArrayList<>(50);
		vses = new ArrayList<>(25);
		system = SYSTEM_ELIMINATION;
		battle_stat = BATTLE_NOT_START;
	}

	public static final int PLAYERS[] = {
			6, 30, 101, 110, 58, 94, 80, 47
	};

	public void init(boolean play) {
		for (int i = 0; i < PLAYERS.length; i++) {
			players.add(PLAYERS[i]);
		}

		if (play) {
			if (Gmud.rand.nextBoolean()) {
				players.set(0, -1);
			} else {
				players.set(1, -1);
			}
		}

//		players.add(6); // 独行大侠
//		players.add(30);  // 顾炎武
//		players.add(101); // 清虚道长
//		players.add(110); // 白瑞德
//		players.add(58);  // 李清照
//		players.add(94);  // 和仲阳
//		players.add(80);  // 余鸿儒
//		players.add(47);  // 王维扬
//		players.add(118); // 封万剑
		circle_over = true;
	}

	String get_players() {
		if (!circle_over)
			return "错误！本轮比赛尚未结束";
		String t = "";
		for (Integer i : players) {
			if (i == -1) {
				t += Gmud.mc.name;
			} else {
				t += Gmud.getNpc(i).name;
			}
			t += '\n';
		}

		return t;
	}

	public String get_vs(int index) {
		vs i = vses.get(index);

		String tt = "";
		if (i.player1 == -1) {
			tt += Gmud.mc.name;
		} else {
			tt += Gmud.getNpc(i.player1).name;
		}

		tt += "　对　";

		tt += Gmud.getNpc(i.player2).name;

		return tt;

	}

	public ArrayList<String> get_vses() {
		ArrayList<String> r = new ArrayList<>();
		String t = "";
		int j = 0;
		for (int i = 0; i < vses.size(); i++) {
			j++;
			String tt = get_vs(i);
			t += tt + "\n";
			if (j >= 4) {
				r.add(t);
				t = "";
				j -= 4;
			}
		}
		if (j > 0) {
			r.add(t);
		}

		return r;
	}

	public void circle_init() {
		vses = new ArrayList<>(25);
		Collections.shuffle(players);
		int t = players.size() / 2;
		for (int i = 0; i < t; i++) {
			vses.add(new vs(players.get(i), players.get(i + t)));
		}
		circle_over = false;
		players.clear();
	}

	public void pop_next_vs() {
		match = vses.get(0);
		vses.remove(0);
		if (vses.isEmpty()) {
			circle_over = true;
		}
	}

	public void next_match_process() {
		if (circle_over) {
			Logger.e("ERROR!");
		}

		if (match.isAttended()) {
			BattleScreen.battle(-1, match.player2, this);
		} else {
			BattleScreen.battle(match.player1, match.player2, this);
		}

	}


	int bet_on = -1;
	final int ON_NONE = -1;
	final int ON_P1 = 0;
	final int ON_P2 = 1;

	int bet_stat = -1;
	final int BET_GLD = 0;
	final int BET_EXP = 1;
	final int BET_POT = 2;
	int bet_count = 0;
	float bet_rate_1 = 0f;
	float bet_rate_2 = 0f;

	public void bet_process() {
		String t = "下一场比赛：";

		if (match.isAttended()) {
			t += " 你";
		} else {
			t += Gmud.getNpc(match.player1).name;
		}
		t += " 对战 " + Gmud.getNpc(match.player2).name;

		t += "，请选择：";

		int adv;
		int mx;

		if (match.isAttended()) {
			adv = Gmud.mc.getNewpj() - Gmud.getNpc(match.player2).getNewpj();
			mx = Math.max(Gmud.mc.getNewpj(), Gmud.getNpc(match.player2).getNewpj());
		} else {
			adv = Gmud.getNpc(match.player1).getNewpj() - Gmud.getNpc(match.player2).getNewpj();
			mx = Math.max(Gmud.getNpc(match.player1).getNewpj(), Gmud.getNpc(match.player2).getNewpj());
		}


		int mom = mx + 10;

		bet_rate_1 = ((float) Math.abs(mx - adv) + 1) / mom / 3f;
		bet_rate_2 = ((float) Math.abs(mx + adv) + 1) / mom / 3f;

		if (bet_rate_1 > bet_rate_2) {
			bet_rate_1 *= 5;
		} else if (bet_rate_2 > bet_rate_1) {
			bet_rate_2 *= 5;
		} else {
			bet_rate_1 *= 3;
			bet_rate_2 *= 3;
		}

		ArrayList<String> s = new ArrayList<>(3);

		if (match.isAttended()) {
			s.add("给自己下注（赔率：1赔" + bet_rate_1 + "）");
			s.add("不下注");
		} else {
			s.add("下注给" + Gmud.getNpc(match.player1).name + "（赔率：1赔" + bet_rate_1 + "）");
			s.add("下注给" + Gmud.getNpc(match.player2).name + "（赔率：1赔" + bet_rate_2 + "）");
			s.add("不下注");
		}
		CScreen scrn = new GeneralMenuScreen(s, t) {

			@Override
			public void onClick(int i) {
				if ((i == 1 && match.isAttended()) || (i == 2)) {
					bet_on = ON_NONE;
					next_match_process();
				} else {
					bet_on = i;
					ArrayList<String> ss = new ArrayList<>(3);
					final int gd = Gmud.mc.gold / 10;
					final int ep = Gmud.mc.exp / 20;
					final int pt = Gmud.mc.potential / 5;

					ss.add("下注" + gd + "金钱");
					ss.add("下注" + ep + "经验");
					ss.add("下注" + pt + "潜能");
					CScreen scrn = new GeneralMenuScreen(ss, "请选择：") {

						@Override
						public void onClick(int i) {
							switch (i) {
								case 0:
									bet_stat = BET_GLD;
									bet_count = gd;
									Gmud.mc.earnMoney(-gd);
									break;
								case 1:
									bet_stat = BET_EXP;
									bet_count = ep;
									Gmud.mc.setExp(Gmud.mc.exp - ep);
									break;
								case 2:
									bet_stat = BET_POT;
									bet_count = pt;
									Gmud.mc.setPotential(Gmud.mc.potential - pt);
									break;
							}
							next_match_process();
						}

						@Override
						public void onCancel() {
							getGame().popScreen();
						}
					};

					scrn.pushToGame();
				}

			}

			@Override
			public void onCancel() {
				getGame().popScreen();
			}
		};

		scrn.pushToGame();
	}


	@Override
	void updato() {
		switch (battle_stat) {
			case BATTLE_NOT_START:
				if (vses.isEmpty()) {
					if (players.size() <= 1) {
						Game.getInstance().data.tournament_finished = true;
						Game.getInstance().wulindahui = null;
						if (players.get(0) == -1) {
							getGame().data.gold_medal++;
							new TalkingScreen("恭喜你获得冠军，你获得了100000金钱奖励！").replaceCurrent();
							Gmud.mc.earnMoney(100000);

//						if(!GmudWorld.game.data.challenge_won)
//							TalkingScreen.ts = new YesNoScreen(instance, "一位神秘高手想要和你对战，是否接受？") {
//
//							@Override
//							protected void onYes() {
//								BattleScreen.battle(-1, Room.vip_npc[1], new FinalChallenge(instance));
//							}
//
//							@Override
//							protected void onNo() {
//								instance.setScreen(GmudWorld.ms);
//							}};

						} else {
							new NotificationScreen(Gmud.getNpc(players.get(0)).name + "获得了武林大会冠军。").replaceCurrent();
						}
					} else {
						circle_init();
						final Tournament t = this;
						new YesNoScreen("本轮比赛开始，是否查看对阵表？") {

							@Override
							protected void onYes() {
								new GeneralLookScreen(get_vses(), t).replaceCurrent();
							}

							@Override
							protected void onNo() {
								getGame().popScreen();
							}
						}.pushToGame();

					}
				} else {
					pop_next_vs();
					BattleScreen.getInstance().holder = this;
					bet_process();

				}
				break;
			case BATTLE_PLAYER1_WIN:
				players.add(match.player1);
				match.refresh();
				battle_stat = BATTLE_NOT_START;
				if (bet_on == ON_P1) {
					String s = "";
					int win = (int) (bet_count * bet_rate_1);
					switch (bet_stat) {
						case BET_EXP:
							s = "经验";
							Gmud.mc.setExp(Gmud.mc.exp + win + bet_count);
							break;
						case BET_POT:
							s = "潜能";
							Gmud.mc.setPotential(Gmud.mc.potential + win + bet_count);
							break;
						case BET_GLD:
							s = "金钱";
							Gmud.mc.earnMoney(win + bet_count);
							break;
					}
					TalkingScreen.ts = this;
					new TalkingScreen("恭喜你从博彩获得了" + win + s).pushToGame();
				}
				break;
			case BATTLE_PLAYER2_WIN:
				players.add(match.player2);
				match.refresh();
				battle_stat = BATTLE_NOT_START;
				if (bet_on == ON_P2) {
					String s = "";
					int win = (int) (bet_count * bet_rate_2);
					switch (bet_stat) {
						case BET_EXP:
							s = "经验";
							Gmud.mc.setExp(Gmud.mc.exp + win + bet_count);
							break;
						case BET_POT:
							s = "潜能";
							Gmud.mc.setPotential(Gmud.mc.potential + win + bet_count);
							break;
						case BET_GLD:
							s = "金钱";
							Gmud.mc.earnMoney(win + bet_count);
							break;
					}
					TalkingScreen.ts = this;
					new TalkingScreen("恭喜你从博彩获得了" + win + s).pushToGame();
				}
				break;
			case BATTLE_RUNAWAY:
				battle_stat = BATTLE_PLAYER2_WIN;
				updato();
				break;
		}

	}

}



